/**
 * 
 */
package com.hsxc.cdz.controller.consoleController;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsxc.cdz.base.bean.ConsoleUser;
import com.hsxc.cdz.mgmt.user.UserManager;

import net.sf.json.JSONObject;

/**
 * @author wpfei 返回对应运营商下的电站管理员
 */
@Controller
@RequestMapping("/api")
public class ConUserRoleController {
	Logger logger = LoggerFactory.getLogger(ConUserRoleController.class);
	@Autowired
    UserManager usermanger;

	@RequestMapping(value = "/stationAdmins", method = RequestMethod.GET)
	@ResponseBody
	public Object getopetators(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			HttpSession httpSession) {
		Subject user = SecurityUtils.getSubject();
		ConsoleUser consoleUser = (ConsoleUser) httpSession.getAttribute("conuser");
		logger.info("username: " + user.getPrincipal().toString());
		Session ses = user.getSession();
		logger.info("session id: " + ses.getId().toString());
		String userID = (String) user.getPrincipal();
		logger.info("USERid" + userID);
		JSONObject result = new JSONObject();
		List<String> stationAdmins = new ArrayList<String>();
		try {
			if (user.hasRole("vendor") || user.hasRole("operator")) {

				stationAdmins = usermanger.findStationAdmin(consoleUser);
			} else {
				logger.info("输出user" + "授权不成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("数据库查找失败");
		}
		result.put("success", true);
		result.put("stationAdmins", stationAdmins);
		return result;
	}
}
